<template>
  <el-card
    class="deal-container"
    v-loading="loading"
    :element-loading-text="loadinText"
  >
    <el-tabs type="card" @tab-click="changeTab" v-model="activeName">
      <el-tab-pane label="我的成交" name="first" class="myDeal">
        <el-table
          height="650"
          :data="myDealList"
          type="border-card"
          style="width: 100%"
        >
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" class="demo-table-expand">
                <el-form-item label="公司名称">
                  <span>{{ props.row.cname }}</span>
                </el-form-item>
                <el-form-item label="业务类型">
                  <span>{{ props.row.cate }}</span>
                </el-form-item>
                <el-form-item label="合同金额">
                  <span>{{ props.row.money }}元</span>
                </el-form-item>
                <el-form-item label="预付金额">
                  <span>{{ props.row.readyPay }}元</span>
                </el-form-item>
                <el-form-item label="签约日期">
                  <span>{{ props.row.date }}</span>
                </el-form-item>
                <el-form-item label="业务员">
                  <span>{{ props.row.uname }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column prop="cname" label="公司名称" width="300">
          </el-table-column>
          <el-table-column
            prop="uname"
            label="业务员"
            width="150"
          ></el-table-column>
          <el-table-column prop="cate" label="业务类型"></el-table-column>
          <template slot="empty">
            <el-empty description="暂无我的成交"></el-empty>
          </template>
        </el-table>
      </el-tab-pane>
      <el-tab-pane
        label="小组成交"
        name="second"
        class="groupDeal"
        v-if="roleId !== '3'"
      >
        <el-table
          height="650"
          :data="groupDealList"
          type="border-card"
          style="width: 100%"
        >
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" class="demo-table-expand">
                <el-form-item label="公司名称">
                  <span>{{ props.row.cname }}</span>
                </el-form-item>
                <el-form-item label="业务类型">
                  <span>{{ props.row.cate }}</span>
                </el-form-item>
                <el-form-item label="合同金额">
                  <span>{{ props.row.money }}元</span>
                </el-form-item>
                <el-form-item label="预付金额">
                  <span>{{ props.row.readyPay }}元</span>
                </el-form-item>
                <el-form-item label="签约日期">
                  <span>{{ props.row.date }}</span>
                </el-form-item>
                <el-form-item label="业务员">
                  <span>{{ props.row.uname }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column prop="cname" label="公司名称" width="300">
          </el-table-column>
          <el-table-column
            prop="uname"
            label="业务员"
            width="150"
          ></el-table-column>
          <el-table-column prop="cate" label="业务类型"></el-table-column>
          <template slot="empty">
            <el-empty description="暂无小组成交"></el-empty>
          </template>
        </el-table>
      </el-tab-pane>
      <el-tab-pane
        label="全部成交"
        name="third"
        class="allDeal"
        v-if="roleId === '1'"
      >
        <el-table
          height="650"
          :data="allDealList"
          type="border-card"
          style="width: 100%"
        >
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" class="demo-table-expand">
                <el-form-item label="公司名称">
                  <span>{{ props.row.cname }}</span>
                </el-form-item>
                <el-form-item label="业务类型">
                  <span>{{ props.row.cate }}</span>
                </el-form-item>
                <el-form-item label="合同金额">
                  <span>{{ props.row.money }}元</span>
                </el-form-item>
                <el-form-item label="预付金额">
                  <span>{{ props.row.readyPay }}元</span>
                </el-form-item>
                <el-form-item label="签约日期">
                  <span>{{ props.row.date }}</span>
                </el-form-item>
                <el-form-item label="业务员">
                  <span>{{ props.row.uname }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column prop="cname" label="公司名称" width="300">
          </el-table-column>
          <el-table-column
            prop="uname"
            label="业务员"
            width="150"
          ></el-table-column>
          <el-table-column prop="cate" label="业务类型"></el-table-column>
          <template slot="empty">
            <el-empty description="暂无全部成交"></el-empty>
          </template>
        </el-table>
      </el-tab-pane>
    </el-tabs>
    <!-- 分页组件 -->
    <div style="height: 60px; margin-top: 19px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[15, 30, 40]"
        :page-size="page_size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </el-card>
</template>

<script>
import {
  getMyDealListApi,
  getGroupDealListApi,
  getAllDealListApi
} from '@/api/company'
export default {
  name: 'deal-page',
  created () {
    this.roleId = localStorage.getItem('crm-roleid')
    this.getMyDealList()
    this.depart_id = localStorage.getItem('crm-departId')
  },
  data () {
    return {
      roleId: 0,
      loading: false,
      loadinText: '',
      page: 1,
      page_size: 15,
      total: 0,
      activeName: 'first',
      // 我的成交 列表
      myDealList: [],
      // 小组成交 列表
      groupDealList: [],
      // 全部成交 列表
      allDealList: [],
      depart_id: 0
    }
  },
  methods: {
    // 获取 我的成交 列表
    async getMyDealList () {
      this.loading = true
      this.loadinText = '数据正在加载中!'
      const { data: res } = await getMyDealListApi(this.page, this.page_size)
      // console.log('获取 我的成交 列表', res)
      this.myDealList = [...res.data.lists]
      this.total = res.data.num
      this.loading = false
      this.loadinText = ''
    },
    // 获取 小组成交 列表
    async getGroupDealList () {
      this.loading = true
      this.loadinText = '数据正在加载中!'
      const { data: res } = await getGroupDealListApi(this.page, this.page_size)
      // console.log('获取 小组成交 列表', res)
      this.groupDealList = [...res.data.lists]
      this.total = res.data.num
      this.loading = false
      this.loadinText = ''
    },
    // 获取 全部成交 列表
    async getAllDealList () {
      this.loading = true
      this.loadinText = '数据正在加载中!'
      const { data: res } = await getAllDealListApi(
        this.page,
        this.page_size,
        this.depart_id
      )
      // console.log('获取 全部成交 列表', res)
      this.allDealList = [...res.data.lists]
      this.total = res.data.num
      this.loading = false
      this.loadinText = ''
    },
    // 切换tab栏 重置页码  判断选择的是哪个tab栏
    changeTab () {
      this.page = 1
      this.page_size = 15
      this.total = 0
      this.activeNameChange()
    },
    // 切换第几页
    handleCurrentChange (val) {
      this.page = val
      this.activeNameChange()
    },
    // 手动更改页码
    handleSizeChange (val) {
      this.page_size = val
      this.activeNameChange()
    },
    // 判断哪个tab栏被选中
    activeNameChange () {
      if (this.activeName === 'first') {
        // console.log('获取 我的成交 列表')
        this.getMyDealList()
      } else if (this.activeName === 'second') {
        // console.log('获取 小组成交 列表')
        this.getGroupDealList()
      } else if (this.activeName === 'third') {
        // console.log('获取 全部成交 列表')
        this.getAllDealList()
      }
    }
  },
  watch: {}
}
</script>

<style lang="scss">
.deal-container {
  box-sizing: border-box;
  height: 837px;
  padding: 25px 40px 0px;
  .el-tabs {
    .el-tabs__item.is-active {
      background-color: #f5f7fa;
    }
  }
}
</style>
